// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-12-23 14:57:37
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Property\PSHELLParser.cpp
 */
#include "PSHELLParser.h"

#include <algorithm>
#include <cstdint>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Property/IPSHELLService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (PSHELL);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/PSHELL/PSHELL.md>
*/

// 以下需要修改
enum PSHELLkey : std::uint8_t
{
    PSHELLKEY_PID  = 2,
    PSHELLKEY_MID1 = 3,
    PSHELLKEY_T    = 4,
    PSHELLKEY_MID2 = 5,
    PSHELLKEY_12T  = 6,
    PSHELLKEY_MID3 = 7,
    PSHELLKEY_TS   = 8,
    PSHELLKEY_NSM  = 9,
    PSHELLKEY_Z1   = 12,
    PSHELLKEY_Z2   = 13,
    PSHELLKEY_MID4 = 14,
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParsePSHELL (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    // 初始化临时的PSHELL数据结构，用于存储解析后的数据
    SG::DataStructure::FEM::PSHELLData tempPSHELL;

    // TODO(bug 001) m_domainId的正确填写，目前默认设置为1
    tempPSHELL.m_domainId = 1;

    // 分割输入字符串EntryInfo，并将其结果存储到dividedEntryInfo

    int  curIDKey = 1;     // 当前处理的字段索引
    bool Z1Empty  = true;  // 用于判断Z1字段是否为空
    bool Z2Empty  = true;  // 用于判断Z2字段是否为空
    bool TEmpty   = true;  // 用于判断厚度T字段是否为空

    // 遍历分割后的字符串条目
    for (auto curStrKey : dividedEntryInfo)
    {
        // 将字符串转换为科学计数法

        // 检查当前字符串是否为空（只包含空格）
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;  // 跳过空字段，继续下一个字段
            continue;
        }

        // 根据当前字段索引解析相应的数据并存储到tempPSHELL
        switch (curIDKey)
        {
        case PSHELLKEY_PID:
            // 解析PID字段，将其转换为长整型并存储到m_id
            tempPSHELL.m_id = convertToI64 (curStrKey);
            break;
        case PSHELLKEY_MID1:
            // 解析MID1字段，将其转换为长整型并存储到m_mId1
            tempPSHELL.m_mId1 = convertToI64 (curStrKey);
            break;
        case PSHELLKEY_T:
            // 解析厚度T字段，将其转换为双精度浮点数并存储到m_thickness
            TEmpty                 = false;
            tempPSHELL.m_thickness = convertToReal (curStrKey);
            break;
        case PSHELLKEY_MID2:
            // 解析MID2字段，将其转换为长整型并存储到m_mId2
            tempPSHELL.m_mId2 = convertToI64 (curStrKey);
            break;
        case PSHELLKEY_12T:
            // 解析1/2T字段，将其转换为双精度浮点数并存储到m_bendModify
            tempPSHELL.m_bendModify = convertToReal (curStrKey);
            break;
        case PSHELLKEY_MID3:
            // 解析MID3字段，将其转换为长整型并存储到m_mId3
            tempPSHELL.m_mId3 = convertToI64 (curStrKey);
            break;
        case PSHELLKEY_TS:
            // 解析TS字段，将其转换为双精度浮点数并存储到m_ts
            tempPSHELL.m_ts = convertToReal (curStrKey);
            break;
        case PSHELLKEY_NSM:
            // 解析非结构质量NSM字段，将其转换为双精度浮点数并存储到m_nsm
            tempPSHELL.m_nsm = convertToReal (curStrKey);
            break;
        case PSHELLKEY_Z1:
            // 解析Z1字段，将其转换为双精度浮点数并存储到m_z[0]
            tempPSHELL.m_z[0] = convertToReal (curStrKey);
            Z1Empty           = false;
            break;
        case PSHELLKEY_Z2:
            // 解析Z2字段，将其转换为双精度浮点数并存储到m_z[1]
            tempPSHELL.m_z[1] = convertToReal (curStrKey);
            Z2Empty           = false;
            break;
        case PSHELLKEY_MID4:
            // 解析MID4字段，将其转换为长整型并存储到m_mId4
            tempPSHELL.m_mId4 = convertToI64 (curStrKey);
            break;
        default:
            // 如果索引不匹配任何已知字段，跳过
            break;
        }

        curIDKey++;  // 处理完当前字段，移动到下一个字段
    }

    // 处理Z1, Z2 和 T的默认值问题
    // 如果Z1为空，则默认值为-0.5倍的厚度
    if (Z1Empty)
    {
        tempPSHELL.m_z[0] = -0.5 * tempPSHELL.m_thickness;
    }
    // 如果Z2为空，则默认值为0.5倍的厚度
    if (Z2Empty)
    {
        tempPSHELL.m_z[1] = 0.5 * tempPSHELL.m_thickness;
    }
    // 如果厚度T为空，则默认值为-1.0
    if (TEmpty)
    {
        tempPSHELL.m_thickness = -1.0;
    }

    // 将解析后的PSHELL数据添加到数据库服务中
    return dbServiceFactory->get<IPSHELLService> ()->Add (tempPSHELL);
}
